/**
 * tag管理-add
 */
var React = require("react");
var {Col,Button,Form,FormGroup,ControlLabel} = require("react-bootstrap");
var {Select,Util,RedStar,FormPage} = require("flower");
class Add extends FormPage{

    constructor(props){
        super(props);
        this.state = {
            addTitle : "新增标签"
        };
    }
    componentDidMount(){
        if(this.props.dataId){
            //编辑
            var param = {
                id:this.props.dataId
            }
            var ret = Util.get("/tag/getById",param);
            console.info(ret);
            this.setState({
                    addTitle:"编辑标签"
                });
            this.setFormData(ret);
        }else{
            //新增
            this.setState({
                addTitle:"新增标签"
            });
        }
    }

    title(){
        var data = [
            {name:"首页",href:"/load/admin/Main.jsx"},
            {name:"标签管理",href:"/load/admin/tag/Index.jsx"},
            {name:this.state.addTitle,href:""}
        ];
        return this.cbb(data);
    }

    content(){
        return <Form horizontal>
                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            标签名称<RedStar/>
                        </Col>
                        <Col sm={8}>
                            <input className="form-control" placeholder="请输入标签名称" ref="name" />
                            <input type="hidden" ref="id"/>
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            标签颜色<RedStar/>
                        </Col>
                        <Col sm={8}>
                            <input className="form-control" placeholder="请输入颜色,eg:css颜色，gray or #dddddd" ref="color" />
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            排序<RedStar/>
                        </Col>
                        <Col sm={2}>
                            <input type="number" className="form-control" ref="sortOrder"/>
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col smOffset={1} sm={8}>
                            <Button type="button" bsStyle="primary" onClick={()=>{
                                console.info(this.getFormData());
                                var data = this.getFormData();
                                if(data.id){
                                    var ret = Util.post("/tag/edit",data);

                                    if(ret.state == 200){
                                        alert("编辑成功!");
                                        Util.goto("/load/admin/tag/Index.jsx");
                                    }else if(ret.state == 300){
                                        var msg = "";
                                        for(var key in ret.msg){
                                            msg += "\r\n"+ret.msg[key];
                                        }
                                        alert("验证未通过！原因："+msg);
                                    }else{
                                        alert("编辑失败！");
                                    }

                                }else{
                                    var ret = Util.post("/tag/add",data);

                                    if(ret.state == 200){
                                        alert("新增成功!");
                                        Util.goto("/load/admin/tag/Index.jsx");
                                    }else if(ret.state == 300){
                                        var msg = "";
                                        for(var key in ret.msg){
                                            msg += "\r\n"+ret.msg[key];
                                        }
                                        alert("验证未通过！原因："+msg);
                                    }else{
                                        alert("新增失败！");
                                    }
                                }

                            }}>
                                提交
                            </Button>
                            <Button type="button" bsStyle="default" style={{marginLeft:"26px"}} onClick={()=>{
                                Util.goto("/load/admin/tag/Index.jsx");
                            }}>
                                返回列表
                            </Button>
                        </Col>
                    </FormGroup>
                </Form>

    }
}
module.exports = Add;